//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections;
using System.Collections.Generic;
using OpenLS.Spreadsheet.Extensibility;

namespace OpenLS.Spreadsheet.Expressions
{
    internal class RangeList : IRange, ICellEnumerator, IEnumerable<object>
    {
        private readonly IRange _left;
        private readonly IRange _right;
        IRange ICellEnumerator.ToRange()
        {
            return this;
        }


        public RangeList(IRange left, IRange right)
        {
            this._left = left;
            this._right = right;
        }

        public IEnumerable<ICell> GetNonEmptyCells()
        {
            foreach (var cell in Left.GetNonEmptyCells())
            {
                yield return cell;
            }
            foreach (var cell in Right.GetNonEmptyCells())
            {
                yield return cell;
            }
        }

        public ICell FirstCell
        {
            get { return Left.FirstCell; }
        }

        public int ColumnSpan
        {
            get { throw new NotImplementedException(); }
        }

        public int RowSpan
        {
            get { throw new NotImplementedException(); }
        }

        public ICell this[int row, int column]
        {
            get { throw new NotImplementedException(); }
        }

        public int RowIndex
        {
            get { throw new NotImplementedException(); }
        }

        public int ColumnIndex
        {
            get { throw new NotImplementedException(); }
        }

        public int Areas
        {
            get { return Left.Areas + Right.Areas; }
        }

        public IRange GetArea(int areaIndex)
        {
            if (areaIndex < Left.Areas)
                return Left.GetArea(areaIndex);
            return
            Right.GetArea(areaIndex - Left.Areas);
        }

        public IWorksheet Worksheet
        {
            get
            {
                return Left.Worksheet;
            }
        }

        public IRange Right
        {
            get { return _right; }
        }

        public IRange Left
        {
            get { return _left; }
        }

        public IRange GetRowRange(int index, RangeType type)
        {
            throw new NotImplementedException();
        }

        public IRange GetColumnRange(int index, RangeType type)
        {
            throw new NotImplementedException();
        }

        public IEnumerator<object> GetEnumerator()
        {
            foreach (var cell in GetNonEmptyCells())
            {
                yield return cell.Value;
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}